package com.cskaoyan.service.impl;


import com.cskaoyan.bean.KeywordListData;
import com.cskaoyan.bean.KeywordParameter;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.model.MarketKeyword;
import com.cskaoyan.model.MarketKeywordExample;
import com.cskaoyan.service.KeywordService;

import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class KeywordServiceImpl implements KeywordService {
    @Override
    public KeywordListData getKeyword(KeywordParameter keywordParameter) {
        KeywordListData keywordListData = new KeywordListData();
        Integer page = keywordParameter.getPage();
        Integer limit = keywordParameter.getLimit();
        String sort = keywordParameter.getSort();
        String order = keywordParameter.getOrder();
        String keyword = keywordParameter.getKeyword();
        String url = keywordParameter.getUrl();

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.setOrderByClause(sort + " " + order);
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria().andDeletedEqualTo(false);
        if(keyword != null){
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if(url != null){
            criteria.andUrlLike("%" + url +"%");
        }
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);
        PageHelper.startPage(page,limit);
        List<MarketKeyword> marketIssues = mapper.selectByExample(marketKeywordExample);

        PageInfo pageInfo = new PageInfo(marketIssues);
        keywordListData.setList(marketIssues);
        keywordListData.setPage(page);
        keywordListData.setTotal((int) pageInfo.getTotal());
        keywordListData.setLimit(limit);
        keywordListData.setPages(pageInfo.getPages());
        sqlSession.commit();
        sqlSession.close();

        return keywordListData;

    }

    @Override
    public MarketKeyword insertNewKeyword(Map<String, String> map) {


        MarketKeyword marketKeyword = new MarketKeyword();
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());
        marketKeyword.setKeyword(map.get("keyword"));
        marketKeyword.setUrl(map.get("url"));
        if ("true".equals(map.get("ishot"))) {
            marketKeyword.setIsHot(true);
        }else {
            marketKeyword.setIsHot(false);
        }
        if("true".equals(map.get("isDefault"))){
            marketKeyword.setIsDefault(true);
        }else {
            marketKeyword.setIsDefault(false);
        }


        int i = mapper.insertSelective(marketKeyword);

        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();

        return marketKeyword;
    }

    @Override
    public MarketKeyword updateKeyword(Map<String, String> map) {
        MarketKeyword marketKeyword = new MarketKeyword();


        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> aClass, Object o) {
                return null;
            }
        }, Date.class);

        try {
            BeanUtils.copyProperties(marketKeyword,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }


        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);


        mapper.updateByPrimaryKeySelective(marketKeyword);
        sqlSession.commit();

        sqlSession.close();

        return marketKeyword;
    }

    @Override
    public void deleteKeyword(Map<String, String> map) {
        MarketKeyword marketKeyword = new MarketKeyword();


        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> aClass, Object o) {
                return null;
            }
        }, Date.class);

        try {
            BeanUtils.copyProperties(marketKeyword,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        marketKeyword.setDeleted(true);

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper mapper = sqlSession.getMapper(MarketKeywordMapper.class);

        mapper.updateByPrimaryKeySelective(marketKeyword);
        sqlSession.commit();
        sqlSession.close();
    }
}
